package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * Connects to the database and inserts either a new a la cart menu row or lunch
 * menu row.
 * 
 * @author Ludvig Gee
 * 
 */

public class NewDb {
    Connection con;
    String restID, dateLunch, dishLunch, DescriptionLunch, priceLunch,
	    typeLunch;
    String dishAla, descriptionAla, priceAla, typeAla;
    int addDone;

    public NewDb() {

    }

    public NewDb(String restID) {

	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	} catch (Exception e) {
	    System.out.println(e);
	    System.out.println("bajs");
	}

    }

    /**
     * Inserts new row into a la carte menu in the database.
     * 
     * @param restID
     * @param dishAla
     *            String from form in edit.jsp
     * @param descriptionAla
     *            String from form in edit.jsp
     * @param priceAla
     *            String from form in edit.jsp
     * @param typeAla
     *            String from form in edit.jsp
     */
    public void NewAla(String restID, String dishAla, String descriptionAla,
	    String priceAla, String typeAla) {

	try {
	    Statement stmt = con.createStatement();

	    String query = "INSERT INTO olofek.menu (Rest_id, Dish, Price, Description, Type) "
		    + "VALUES ('"
		    + restID
		    + "','"
		    + dishAla
		    + "', '"
		    + priceAla
		    + "', "
		    + "'"
		    + descriptionAla
		    + "', '"
		    + typeAla + "')";
	    addDone = 1;
	    stmt.executeUpdate(query);
	} catch (Exception e) {
	    System.out.println(e);
	}

    }

    /**
     * Inserts new row into a la carte menu in the database.
     * 
     * @param restID
     * @param dateLunch
     *            String from form in edit.jsp
     * @param dishLunch
     *            String from form in edit.jsp
     * @param descriptionLunch
     *            String from form in edit.jsp
     * @param priceLunch
     *            String from form in edit.jsp
     * @param typeLunch
     *            String from form in edit.jsp
     */

    public void NewLunch(String restID, String dateLunch, String dishLunch,
	    String descriptionLunch, String priceLunch, String typeLunch) {

	try {
	    Statement stmt = con.createStatement();

	    String query = "INSERT INTO olofek.lunch (Date, rest_id, Dish, Price, Description, Type) "
		    + "VALUES ('"
		    + dateLunch
		    + "','"
		    + restID
		    + "', '"
		    + dishLunch
		    + "', "
		    + "'"
		    + priceLunch
		    + "', '"
		    + descriptionLunch + "', '" + typeLunch + "')";
	    addDone = 1;
	    stmt.executeUpdate(query);
	} catch (Exception e) {
	    System.out.println(e);
	}

    }

    public String getRestID() {
	return restID;
    }

    /**
     * parameter to signify successfully adding new row.
     * 
     * @return int set depending if query is made.
     */
    public int getAddDone() {
	return addDone;
    }
}
